package com.dd.census.controller;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.SysUser;
import com.dd.census.entity.SysUserLog;
import com.dd.census.service.SysUserService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.validator.ValidateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value = "/rest/userLogin")
public class SysUserLoginController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SysUserLoginController.class);

	@Autowired
	private SysUserService userService;

	@Autowired
	private ObjectMapper objectMapper;
	
	private String  queue ="local.log.user.queue";
	private String  exchange ="local.log.user.exchange";
	private String  key ="local.log.user.routing.key";

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ResponseEntity<Object> login(@RequestBody SysUser user, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(user);
			
			SysUserLog userLog=new SysUserLog(user.getAccount(),"Login","login",objectMapper.writeValueAsString(user));
            userLog.setCreateTime(new Date());

			return this.doSuccess(request, userService.selectLogin(user));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/logout", method = RequestMethod.PUT)
	public ResponseEntity<Object> logout(
				@RequestHeader("token")String token, 
				@RequestBody SysUser user, 
				HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(user);

			// 查询用员工信息
			SysUser userNew = userService.selectById(user.getId());
			if (null == userNew) {
				throw new SysException(HttpStatus.NOT_FOUND);
			}

			// 主操作时验证认证Token和保存Token是否一致
			String tokenKey = TokenHelper.validate(user, token);
			

			// 删除Token
			if ( !"".equals(tokenKey) ) {
				TokenHelper.delete(tokenKey);
			}

			return this.doSuccess(request);

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("updatePwd")
	public ResponseEntity<Object> updatePassword(@RequestBody SysUser user,HttpServletRequest request){
		try {
			
			return this.doSuccess(request,userService.updatePassword(user));
		}catch(Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}